Ex003, Timer
Timer component is used in Delphi to do certain tasks each period of time such as
playing alarm sound at each hour, or getting data from a database server each amount
of time, etc.
The most important timer properties are: Interval and Enabled. Interval used to
set a time in milliseconds; if you want to let some event happen each second you
must assign 1000 to Interval, two seconds 2*1000 and so on. Enabled used to switch
timer on and off. If you want to run the event only one time, set Enabled to True
and when the event happened set Enabled to False to disable timer event.
Exercise 003: Timer
1. Drop a Timer (from System page).
2. Drop a Label and name it 'laTime'.
3. Point on Timer, press F11, click events tab at timer's object inspector.
4. Double click on OnTimer event and write this code:
laTime.Caption:= DateTimeToStr(Now);
5. Run the program
If you want to display only time write:
TimeToStr(Now)
or you can write:
TimeToStr(Time);
If you want to display only date write:
DateToStr(Now)
or you can write:
DateToStr(Date)
Notes:
Date and time format will be displayed according to Windows data and time settings.
If you want to change date and time format use FormatDateTime function:
laTime.Caption:=
FormatDateTime('ddd dd/mmm/yyy HH:nn:ss', Now);
ddd displays day string such as Saturday
dd displays day number such as 23
mmm displays month string such as November
mm displays month number such as 11
yyy displays long year such as 1999
yy displays short year such as 99
hh displays 24-hour system (without using am/pm) such as 14
am/pm displays 12-hour system such as 2:20:35 pm
See also:
Date and time routines